package com.fate.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.model.builder.AlipayTradePayRequestBuilder;
import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
import com.alipay.demo.trade.model.builder.AlipayTradeQueryRequestBuilder;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import com.alipay.demo.trade.model.result.AlipayF2FQueryResult;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.fate.config.AliProperties;
import com.fate.feign.PayOrderFeign;
import com.fate.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class PayServiceImpl implements PayService {

    @Autowired
    private AlipayTradeService alipayTradeService;
    @Autowired
    private AliProperties aliProperties;
    @Autowired
    private PayOrderFeign payOrderFeign;
    /**
     * 创建二维码的接口
     * @param map
     * @return
     */
    @Override
    public String createPrePay(Map<String, String> map) {
        //根据一些条件创建 金额 名称
        String orderNumber =  map.get("orderNumber");

        //远程调用order-service


        //获取到远程信息
        AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder();
        AlipayF2FPrecreateResult result = alipayTradeService.tradePrecreate(builder);
        switch(result.getTradeStatus()){
            case SUCCESS:
                //下预订单成功
                AlipayTradePrecreateResponse response = result.getResponse();
                String qrCode = response.getQrCode();
                System.out.println(qrCode);
                return qrCode;
            case FAILED:
            case UNKNOWN:
                log.error("创建预订单失败,订单号为{}"+orderNumber);
                    throw new RuntimeException("创建预订单失败");

            default:
                return "失败";
        }

    }

    /**
     * 支付宝的验签
     * @param map
     * @return
     */
    @Override
    public Boolean resCheck(Map<String, String> map) {
        boolean flag = false;
        try {
            flag = AlipaySignature.rsaCheckV1(map, aliProperties.getAliPublicKey(), "UTF-8", aliProperties.getSignType());
            System.out.println(flag);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改订单状态 已经支付
     */
    @Override
    public void changeOrderStatus(String orderNum) {
        payOrderFeign.changeOrderStatus(orderNum);
    }

    /**
     * 查询ali那边是否成功
     * @param orderNum
     * @return
     */
    @Override
    public Boolean queryAliOrderPay(String orderNum) {
        AlipayTradeQueryRequestBuilder builder = new AlipayTradeQueryRequestBuilder();
        builder.setOutTradeNo(orderNum);
        //根据商户订单号 查询是否支付成功
        AlipayF2FQueryResult queryResult = alipayTradeService.queryTradeResult(builder);
        return queryResult.isTradeSuccess();
    }
}
